import storageApi from './core/storage/storage';

export default function runBlock($log, $rootScope, $state) {
  'ngInject';

  // logging state interceptor
  $rootScope.$on('$stateChangeStart', (event, toState, toParams, fromState, fromParams) => {
    if (toState.name === 'login') return; // ignore login state

    if (!storageApi.get('orgId')) {
      event.preventDefault();
      $state.go('login', {
        from: toState.name,
        w: 'notLogin',
      });
      return;
    }

    $rootScope.orgId = storageApi.get('currentOrgId') || storageApi.get('orgId');
    // if (!$rootScope.permissionList) {
    //   $rootScope.permissionList = $rootScope.permissionList ||
    // JSON.parse(storageApi.get('permissionList'));
    // }
  });

  /*eslint-disable */
  const activeDiv = document.getElementsByClassName('icon-item fr active');
  document.body.onclick = function(e){
    e = e || window.event;
    var target = e.target || e.srcElement;
    if(target !== activeDiv){
      $rootScope.currentIndex = '';
    }
  }
}
